package cn.iocoder.yudao.module.member.api.user;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import cn.iocoder.yudao.module.member.convert.user.MemberUserConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.dal.mysql.user.MemberUserMapper;
import cn.iocoder.yudao.module.member.service.user.MemberUserService;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.USER_MOBILE_NOT_EXISTS;

/**
 * 会员用户的 API 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class MemberUserApiImpl implements MemberUserApi {

    @Resource
    private MemberUserService userService;

    @Resource
    MemberUserMapper memberUserMapper;

    @Override
    public MemberUserRespDTO getUser(Long id) {
        MemberUserDO user = userService.getUser(id);
        return MemberUserConvert.INSTANCE.convert2(user);
    }

    @Override
    public List<MemberUserRespDTO> getUserList(Collection<Long> ids) {
        return MemberUserConvert.INSTANCE.convertList2(userService.getUserList(ids));
    }

    @Override
    public List<MemberUserRespDTO> getUserListByNickname(String nickname) {
        return MemberUserConvert.INSTANCE.convertList2(userService.getUserListByNickname(nickname));
    }

    @Override
    public MemberUserRespDTO getUserByMobile(String mobile) {
        return MemberUserConvert.INSTANCE.convert2(userService.getUserByMobile(mobile));
    }

    @Override
    public MemberUserRespDTO getUserByIdCard(String idCard) {
        return MemberUserConvert.INSTANCE.convert2(memberUserMapper.selectOne(MemberUserDO::getIdCard, idCard));
    }

    @Override
    public void validateUser(Long id) {
        MemberUserDO user = userService.getUser(id);
        if (user == null) {
            throw exception(USER_MOBILE_NOT_EXISTS);
        }
    }

    @Override
    public Long userCount() {
        return memberUserMapper.selectCount();
    }

    @Override
    public Long lifeUserCount() {
        return memberUserMapper.selectCount(new LambdaQueryWrapperX<MemberUserDO>()
                .gt(MemberUserDO::getLoginDate, LocalDate.now().plusDays(-30))
        );
    }

    @Override
    public ChartData getNewUserChartData(LocalDateTime startTime, LocalDateTime endTime) {
        List<String> xData = new ArrayList<>();
        List<Long> yData = new ArrayList<>();

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM-dd");

        // 按天遍历时间范围
        LocalDateTime currentDate = startTime.toLocalDate().atStartOfDay();
        LocalDateTime endDate = endTime.toLocalDate().atStartOfDay();

        while (!currentDate.isAfter(endDate)) {
            // 格式化日期为 MM-dd 格式
            String dateStr = currentDate.format(formatter);
            xData.add(dateStr);

            // 查询当天新增用户数量
            LocalDateTime dayStart = currentDate;
            LocalDateTime dayEnd = currentDate.plusDays(1);

            Long count = memberUserMapper.selectCount(new LambdaQueryWrapperX<MemberUserDO>()
                    .ge(MemberUserDO::getCreateTime, dayStart)
                    .lt(MemberUserDO::getCreateTime, dayEnd)
            );

            yData.add(count != null ? count : 0L);

            // 移动到下一天
            currentDate = currentDate.plusDays(1);
        }

        return new ChartData(xData, yData);
    }
}
